<?php

namespace App\Models\CacheModel\zl2;

use App\Models\CacheModel\AppConfig;
use App\Models\CacheModel\FortuneCache;
use App\Models\CacheModel\MonthFortune;
use App\Models\CacheModel\Recode;
use App\Models\Api\AspectChange;
use App\Models\CacheModel\AstroChange;
use App\Tools\Astrolog\DailyFortune;
use App\Tools\Astrolog\FortunesChange;
use App\Exceptions\ApiException;
use App\Tools\Util;
use App\Tools\Service\AstroFortuneSurprise;
use DB as Db;
use env;
use Log;
use AstroTool\Cache\CacheManage;

class AstroFortuneChange
{
    protected $cacheModel = 'astro_fortune.astro_fortune_change_content';

    protected $recodeInfo = [];
    protected $unlockMonthData = null;
    protected $timeNoteData = null;

    public function getTimeRange()
    {
        //$config = (new AppConfig())->configInfo('astro_fortune_change');
        //todo
        $config='';
        return [
            'min' => (int)($config['min_time'] ?? 1662739200),
            'max' => (int)($config['max_time'] ?? 1893427199),
        ];
    }

    /**
     * 获取 计算 星运变动 信息
     * @param int $time
     * @return array|mixed
     * @throws \Exception
     */
    public function getAstroFortuneChangeData(int $time)
    {
        $cacheKey = date('Ymd', $time);
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('all', $cacheKey)->setCacheType('string')
            ->getDataAndSet(function () use ($cacheKey, $time)
            {
                try
                {
                    $astroFortuneChangeData = DailyFortune::getInstance()
                        ->setAdvance(date('Y-m-d 00:00:00', $time))
                        ->setChangeType(1)
                        ->sensitivity();

                    $d = [];
                    foreach ($astroFortuneChangeData as $item)
                    {
                        if (isset($item['recommend_type']))
                        {   // 功能推荐
                            $item['data_type'] = 2; // 数据类型 1=>天象信息  2功能推荐信息
                            $d[] = $item;
                        } else
                        {
                            $d[] = [
                                'data_type' => 1,
                                'tag' => $item['tag'],
                                'title' => $item['title'],
                                'score' => null,  // 感受度（仅天象有值）  没有档案 ｜ 不显示感受度
                                'time' => $item['time'],
                                'st' => $item['start_time'] ?? 0,   // 天象开始时间
                                'et' => $item['end_time'] ?? 0, // 天象结束时间
                                'state' => (int)$item['state'],   // 1=>起始 2=>精准 3=>结束（仅逆行、相位）
                                'code' => (int)$item['code'],
                                'status' => (int)$item['status'],  // 1=>好（大笑） 2=>平（微笑） 3=>坏（哭）
                            ];
                        }
                    }

                    CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('all', $cacheKey)->setCacheType('string');
                    return json_encode($d, JSON_UNESCAPED_UNICODE);
                }
                catch (\Exception $e)
                {
                      Log::error('AstroFortuneChange model => getAstroFortuneChangeData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine());
                    return [];
                }
            }, [], 'all');

        return $data ? json_decode($data, true) : [];
    }

    /**
     * 获取 计算 星运变动 进行中 信息
     * @param int $time
     * @return array|mixed
     * @throws \Exception
     */
    public function getAstroFortuneChangeUnderwayAstroData(int $time)
    {
        $cacheKey = date('Ymd', $time);
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('underway', $cacheKey)->setCacheType('string')
            ->getDataAndSet(function () use ($cacheKey, $time)
            {
                try
                {
                    $astroFortuneChangeData = DailyFortune::getInstance()
                        ->setAdvance(date('Y-m-d 00:00:00', $time))
                        ->setChangeType(3)
                        ->sensitivity();
                    $d = [];
                    foreach ($astroFortuneChangeData as $item)
                    {
                        $d[] = [
                            'tag' => $item['tag'],
                            'title' => $item['title'],
                            'score' => null,  // 感受度（仅天象有值）
                            'st' => $item['start_time'] ?? 0,   // 天象开始时间
                            'et' => $item['end_time'] ?? 0, // 天象结束时间
                            'time_des' => sprintf("%s-%s", date('m.d', $item['start_time']), date('m.d', $item['end_time'])),
                            'code' => (int)$item['code'],
                            'status' => (int)$item['status'],  // 1=>好（大笑） 2=>平（微笑） 3=>坏（哭）
                        ];
                    }

                    CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('underway', $cacheKey)->setCacheTime(300)->setCacheType('string');
                    return json_encode($d, JSON_UNESCAPED_UNICODE);
                }
                catch (\Exception $e)
                {
                        Log::error('AstroFortuneChange model => getAstroFortuneChangeUnderwayAstroData()异常：' . $e->getMessage() . ' 失败行数：' . $e->getLine());
                    return [];
                }
            }, [], 'underway');

        return $data ? json_decode($data, true) : [];
    }

    /**
     * 获取星运变动
     * @param int $uid
     * @param array $recodeInfo 档案信息
     * @param int $time 获取 指定日期零点【时间戳】 的 星运变动
     * @param int $nowTime0 今天零点时间
     * @param int $type 模式 1正常  2纯净
     * @return array
     * @throws \Exception
     */
    public function getUserAstroFortuneChangeDataResert(int $uid, array $recodeInfo, int $time, int $nowTime0, int $type): array
    {
        $ItemDate = date('Y-m-d', $time);
        $maxTime = $nowTime0 + 86400 * 2; // 后天零点时间
        $astroFortuneChangeInfo = $recodeInfo ? (new Zl2AstrFortuneContent())->getUserAstroFortuneChangeData($uid, $recodeInfo, $time, 1) : [];
        if ($astroFortuneChangeInfo)
        {   // 开始解锁了
            // 档案是否更新
            $recodeIsChange = $astroFortuneChangeInfo['recode_is_change'] ? 1 : 0;
            if ($astroFortuneChangeInfo['status'] === 1)
            {   // 已解锁
                $data = $astroFortuneChangeInfo['content'];
                // 计算状态
                $status = 2;    // 已解锁
            } else
            {
                $data = $this->getAstroFortuneChangeData($time);
                $status = 1;    // 计算中

                if ($astroFortuneChangeInfo['status'] === 0)
                {   // 进行中
                    if (time() - $astroFortuneChangeInfo['create_time'] > 900)
                    {   // 15分钟后还未解锁成功
                        // 重新发送 计算队列
                        Util::queuePush('astro_fortune_change_calculate', ['id' => $astroFortuneChangeInfo['id'], 'recode_info' => $recodeInfo], 0);
                    }
                } elseif ($astroFortuneChangeInfo['status'] === 2)
                {   // 计算失败
                    // 重新发送 计算队列
                    Util::queuePush('astro_fortune_change_calculate', ['id' => $astroFortuneChangeInfo['id'], 'recode_info' => $recodeInfo], 0);
                }
            }
        } else
        {   // 未计算过
            $data = $this->getAstroFortuneChangeData($time);
            $recodeIsChange = 0;
            $status = 0;
        }

        $codes = array_column($data, 'code');
        $codeTexts = (new Zl2AstrFortuneText())->batchGetInfoByIds($codes, ['code', 'explain', 'opportunity', 'challenge', 'extend', 'zan1', 'zan2', 'title']);
        $zanDatas = (new Zl2AstrFortuneTextZan())->getZan($uid);
        $resData = [];
        $luckAdversity = [
            1 => ['name' => '困难', 'color' => '#7775ff',],
            2 => ['name' => '警戒', 'color' => '#ffa536',],
            3 => ['name' => '平平', 'color' => '#ffed17',],
            4 => ['name' => '吉利', 'color' => '#00f4e7',],
            5 => ['name' => '大吉', 'color' => '#00e9a0',],
        ];
        $envName = env('host.name');
        foreach ($data as $itemData)
        {
            if ($type === 2 && $itemData['data_type'] === 2) continue;

            if ($itemData['data_type'] === 1)
            {   // 数据类型 1=>天象信息
                $isZan = isset($zanDatas[$itemData['code']]) ? (int)$zanDatas[$itemData['code']] : 0;
                if (!isset($codeTexts[$itemData['code']]))
                {
                    if ($envName === 'online')
                    {   // 词条缺失 不显示
                        continue;
                    }
                    $hideData = [
                        [
                            'hide_type' => 1,
                            'data' => [
                                'title' => '解读',
                                'content' => '词条缺失code=' . $itemData['code'],
                            ]
                        ]
                    ];
                    $keywords = [
                        [
                            'name' => '缺失',
                            'color' => $luckAdversity[1]['color'],
                        ]
                    ];
                    $tipIcon = $itemData['tag'] === '天象' ? '/test/astro_fortune/astro_status1.png' : '/test/astro_fortune/fortune_status1.png';
                } else
                {
                    $tipIcon = $itemData['tag'] === '天象' ? "/test/astro_fortune/astro_status{$itemData['status']}.png" : "/test/astro_fortune/fortune_status{$itemData['status']}.png";
                    $keywords = [];
                    foreach ($codeTexts[$itemData['code']]['extend'] as $keywordsItem)
                    {
                        if ($keywordsItem['title'])
                        {
                            $keywords[] = [
                                'name' => $keywordsItem['title'],
                                'color' => $luckAdversity[$keywordsItem['type']]['color'],
                            ];
                        }
                    }

                    $hideData = [];
                    if ($codeTexts[$itemData['code']]['explain'])
                    {
                        $hideData[] = [
                            'hide_type' => 1,
                            'data' => [
                                'title' => '解读',
                                'content' => $codeTexts[$itemData['code']]['explain'],
                            ]
                        ];
                    }
                    if ($codeTexts[$itemData['code']]['opportunity'])
                    {
                        $hideData[] = [
                            'hide_type' => 1,
                            'data' => [
                                'title' => '机遇',
                                'content' => $codeTexts[$itemData['code']]['opportunity'],
                            ]
                        ];
                    }
                    if ($codeTexts[$itemData['code']]['challenge'])
                    {
                        $hideData[] = [
                            'hide_type' => 1,
                            'data' => [
                                'title' => '挑战',
                                'content' => $codeTexts[$itemData['code']]['challenge'],
                            ]
                        ];
                    }
                    if ($itemData['score'] !== null)
                    {
                        $des = '';
//                        if ($itemData['state'] === 3 && $itemData['st'] > $nowTime)
//                        {   // 结束的时候   并且 当前天象已经开始了
//                            $des = sprintf("还剩d%天", (int)(($itemData['et'] - $nowTime) / 86400));
//                        }
                        $hideData[] = [
                            'hide_type' => 3,
                            'data' => [
                                'title' => '你的感受度：',
                                'score' => (int)($itemData['score'] * 100),
                                'des' => $des,
                            ]
                        ];
                    }
                    if ($itemData['time'] < $maxTime)
                    {   // 后天以后的时间   不显示
                        $hideData[] = [
                            'hide_type' => 4,
                            'data' => [
                                'id' => $itemData['code'],
                                'zan_num1' => $codeTexts[$itemData['code']]['zan1'],
                                'zan_num2' => $codeTexts[$itemData['code']]['zan2'],
                                'is_zan' => $isZan,
                                'target' => 'local',
                                'link' => "astro_foutune_change_detail?id={$itemData['code']}&status={$itemData['status']}&score={$itemData['score']}&date={$ItemDate}&need_auth=2",
                            ]
                        ];
                    }
                }

                $resData[] = [
                    'type' => 1,
                    'list_item' => [
                        'timestamp' => $itemData['time'],
                        'time' => date('H:i', $itemData['time']),
                        'des' => $itemData['state'] === 1 ? '开始' : '结束',
                        'surprised_img' => '',
                        'label' => $itemData['state'] === 1 ? ['text' => '始', 'bg_color' => '#4BEA9C',] : ['text' => '终', 'bg_color' => '#FFB500',],
                        'content' => [
                            [
                                'type' => 1,
                                'data' => [
                                    'tip_icon' => $tipIcon,
                                    'list' => $keywords,
                                ]
                            ], [
                                'type' => 4,
                                'data' => [
                                    'style' => 1,
                                    'tag' => $itemData['tag'],
                                    'content' => $itemData['title'],
                                    'img' => '',
                                    'title' => '',
                                    'read_num' => -1,
                                    'synastry_num' => -1,
                                    'hide_data' => $hideData,
                                ]
                            ],
                        ],
                    ]
                ];
            } else
            {   // 数据类型 2功能推荐信息
//                [
//                    'recommend_type' => 6,
//                    'name' => '合盘恋爱报告',
//                    'text' => '人与人之间的相处非常复杂，了解你们之间的关系走向以及互动特点，对您驾驭这段关系定能有所帮助。',
//                    'target' => 'local',
//                    'link' => '',
//                    'time' => '',
//                ]
                $resData[] = [
                    'type' => 1,
                    'list_item' => [
                        'timestamp' => $itemData['time'],
                        'time' => '',
                        'des' => '',
                        'surprised_img' => '',
                        'label' => null,
                        'content' => [
                            [
                                'type' => 2,
                                'data' => [
                                    'icon' => '',
                                    'title' => $itemData['name'],
                                    'tip_icon' => '/test/astro_fortune/recommented_tip_icon.png',
                                ]
                            ], [
                                'type' => 3,
                                'data' => [
                                    'content' => $itemData['text'],
                                    'target' => '',
                                    'link' => '',
                                    'is_arrow' => 0,
                                    'sign_name' => '',
                                    'relation_name' => '',
                                ]
                            ], [
                                'type' => 7,
                                'data' => [
                                    'text' => '前往查看',
                                    'target' => $itemData['target'],
                                    'link' => $itemData['link'],
                                ]
                            ],
                        ],
                    ]
                ];
            }
        }

        return [
            'status' => $status,
            'recode_is_change' => $recodeIsChange,
            'data' => $resData,
        ];
    }

    /**
     * 获取星运变动 详情
     * @param int $uid
     * @param array $recodeInfo
     * @param int $afcCode
     * @param int $status
     * @param int $dateTime
     * @param float|null $score
     * @return array
     * @throws \Exception
     */
    public function getAstroFortuneChangeUnderwayData(int $uid, array $recodeInfo, int $afcCode, int $status, int $dateTime, float $score = null): array
    {
        $astroFortuneChangeInfo = $recodeInfo ? (new Zl2AstrFortuneContent())->getUserAstroFortuneChangeData($uid, $recodeInfo, $dateTime, 1) : [];
        if ($astroFortuneChangeInfo && $astroFortuneChangeInfo['status'] === 1)
        {   // 计算过
            $data1 = $astroFortuneChangeInfo['content'];
        } else
        {   // 未计算过
            // 获取今天的星运变动信息
            $data1 = $this->getAstroFortuneChangeData($dateTime);
        }

        $afTypeConfig = [1 => '天象', 2 => '行运', 3 => '三限',];
        $afType = ((string)$afcCode)[0];
        $data[] = [
            'tag' => $afTypeConfig[$afType],
            'time' => 0,
            'st' => 0,
            'et' => 0,
            'state' => 0,
            'code' => $afcCode,
            'score' => $score,
            'status' => $status,
            'data_type' => 1
        ];
        $data = array_merge($data, $data1);
        $codes = array_values(array_unique(array_column($data, 'code')));

        $codeTexts = (new Zl2AstrFortuneText())->batchGetInfoByIds($codes, ['code', 'explain', 'opportunity', 'challenge', 'extend', 'zan1', 'zan2', 'title']);
        $resData = [];
        $luckAdversity = [
            1 => ['name' => '困难', 'color' => '#7775ff',],
            2 => ['name' => '警戒', 'color' => '#ffa536',],
            3 => ['name' => '平平', 'color' => '#ffed17',],
            4 => ['name' => '吉利', 'color' => '#00f4e7',],
            5 => ['name' => '大吉', 'color' => '#00e9a0',],
        ];

        $zanDatas = (new Zl2AstrFortuneTextZan())->getZan($uid);
        $issetCode = [];
        $envName = env('host.name');
        foreach ($data as $itemData)
        {
            if ($itemData['data_type'] === 2 || !$itemData['code'] || isset($issetCode[$itemData['code']])) continue;
            $isZan = isset($zanDatas[$itemData['code']]) ? (int)$zanDatas[$itemData['code']] : 0;
            if (!isset($codeTexts[$itemData['code']]))
            {
                if ($envName === 'online')
                {   // 词条缺失 不显示
                    continue;
                }
                $hideData = [
                    [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => '词条缺失code=' . $itemData['code'],
                        ]
                    ]
                ];
                $keywords = [
                    [
                        'name' => '缺失',
                        'color' => $luckAdversity[1]['color'],
                    ]
                ];
                $tipIcon = $itemData['tag'] === '天象' ? '/test/astro_fortune/astro_status1.png' : '/test/astro_fortune/fortune_status1.png';
            } else
            {
                $tipIcon = $itemData['tag'] === '天象' ? "/test/astro_fortune/astro_status{$itemData['status']}.png" : "/test/astro_fortune/fortune_status{$itemData['status']}.png";
                $keywords = [];
                foreach ($codeTexts[$itemData['code']]['extend'] as $keywordsItem)
                {
                    if ($keywordsItem['title'])
                    {
                        $keywords[] = [
                            'name' => $keywordsItem['title'],
                            'color' => $luckAdversity[$keywordsItem['type']]['color'],
                        ];
                    }
                }

                $hideData = [];
                if ($codeTexts[$itemData['code']]['explain'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => $codeTexts[$itemData['code']]['explain'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['opportunity'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '机遇',
                            'content' => $codeTexts[$itemData['code']]['opportunity'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['challenge'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '挑战',
                            'content' => $codeTexts[$itemData['code']]['challenge'],
                        ]
                    ];
                }
                if ($itemData['score'] !== null)
                {
                    $des = '';
//                        if ($itemData['state'] === 3 && $itemData['st'] > $nowTime)
//                        {   // 结束的时候   并且 当前天象已经开始了
//                            $des = sprintf("还剩d%天", (int)(($itemData['et'] - $nowTime) / 86400));
//                        }
                    $hideData[] = [
                        'hide_type' => 3,
                        'data' => [
                            'title' => '你的感受度：',
                            'score' => (int)($itemData['score'] * 100),
                            'des' => $des,
                        ]
                    ];
                }
            }
            $issetCode[$itemData['code']] = $itemData['code'];
            $resData[] = [
                'id' => $itemData['code'],
                'tip_icon' => $tipIcon,
                'list' => $keywords,
                'tag' => $itemData['tag'],
                'content' => $codeTexts[$itemData['code']]['title'] ?? '缺失',
                'hide_data' => $hideData,
                'zan_num1' => $codeTexts[$itemData['code']]['zan1'] ?? 0,
                'zan_num2' => $codeTexts[$itemData['code']]['zan2'] ?? 0,
                'is_zan' => $isZan,
            ];
        }

        return $resData;
    }

    /**
     * 获取首页天象信息
     * @param int $nowTime
     * @param bool $isYoung 是否是青少年模式
     * @return array
     * @throws \Exception
     */
    public function getIndexAstroFortuneChangeData(int $nowTime, bool $isYoung)
    {
        $cackeKeyId = date('YmdH', $nowTime); // 每小时 计算一次  缓存有效期 3610秒
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('index', $cackeKeyId)->setCacheTime(3610)
            ->setCacheType('string')
            ->getDataAndSet(function () use ($nowTime)
            {
                try
                {
                    $planets = ['太阳', '月亮', '水星', '金星', '火星', '木星', '土星', '天王星', '海王星', '冥王星', 17 => '上升', 18 => '天底', 19 => '下降', 20 => '天顶'];
                    $phases = [0 => '合', 60 => '半拱', 90 => '刑', 120 => '拱', 180 => '冲'];
                    $date1 = date('Y-m-d H:00:00', $nowTime - 28800);   // 当前时间 0时区时间
                    $date2 = date('Y-m-d H:00:00', $nowTime + 86400 - 28800);

                    $data1 = AspectChange::whereBetween('time', [$date1, $date2])->whereIn('state', [1, 3])->get()->toArray();
                    $data2 = AstroChange::whereBetween('time', [$date1, $date2])->whereIn('type', [1, 2, 3, 4])->get()->toArray();
                    $dataAll = array_merge($data1, $data2);
                    $resData = [];
                    // key=> 1.行星换座、2.行星顺逆行变换、3.日食、4.月食 7相位
                    // value=>词条二级类型 1=>相位 2=>换宫 3=>换座 4=>顺逆行 5=>日食/月食
                    $type = [
                        1 => 3,
                        2 => 4,
                        3 => 5,
                        4 => 5,
                        7 => 1,
                    ];
                    foreach ($dataAll as $datum)
                    {

                        $item = DailyFortune::getInstance()->transerData($datum);
                        $code = '';
                        switch ($datum['type'])
                        {
                            case 1: // 换座
                                $title = $item['text'];
                                break;
                            case 2: // 顺逆
                                $txt = $datum['speed'] == 1 ? '结束' : '开始'; // 1=>顺行 2=>逆行
                                $title = $item['text'] . $txt;
                                break;
                            case 3: // 日食
                            case 4: // 月食
                                $extend = json_decode($datum['extend'], true);
                                $resData[] = [
                                    'code' => '',
                                    'time' => $extend['end'] + 28800,   // 转换成东八区时间
                                    'title' => $item['text'] . '结束',
                                    'status' => $item['status'],
                                ];
                                $title = $item['text'] . '开始';
                                break;
                            case 7: // 相位
//                                $code = sprintf("%01d%01d%02d%03d%02d", 1, $type[$datum['type']], $datum['pid1'], $datum['phase'], $datum['pid2']);
//                                $title = $item['text'] . ($datum['state'] == 1 ? '开始' : '结束');
//                                $title = $planets[$datum['pid1']] . ($datum['state'] == 1 ? '开始' : '结束') . $phases[$datum['phase']] . $planets[$datum['pid2']];
                                $title = $planets[$datum['pid1']] . $phases[$datum['phase']] . $planets[$datum['pid2']] . ($datum['state'] == 1 ? '开始' : '结束');
                                break;
                            default:
                                $title = '';
                        }

                        $resData[] = [
//                            'code' => $code,
                            'type' => $type[$datum['type']],    // 词条二级类型 1=>相位 2=>换宫 3=>换座 4=>顺逆行 5=>日食/月食
                            'time' => strtotime($datum['time'] . '+8hour'), // 0时区时间转换成 东八区时间
                            'title' => $title,
                            'status' => $item['status'],
                        ];
                    }

                    $resData = resort3($resData, 'time', 'asc');

                    return json_encode($resData, JSON_UNESCAPED_UNICODE);
                }
                catch (\Exception $e)
                {
                    Log::write('AstroFortuneChange model => getIndexAstroFortuneChangeData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                    return [];
                }
            }, [], 'index');

        $data = $data ? json_decode($data, true) : [];
        $resData = [];
        $color = [
            1 => '#00E9A0',
            2 => '#FFED17',
            3 => '#7775FF',
        ];

        foreach ($data as $datum)
        {
            $title = $datum['title'];
            $resData[] = [
                'time' => date('H:i', $datum['time']),
                'color' => $color[$datum['status']] ?? '#00E9A0',
                'title' => $title,
                'target' => 'local',
                'link' => 'astro_foutune_change_list?tab_index=0&need_auth=' . ($isYoung ? 3 : 0),
            ];
        }

        if (empty($resData))
        {
            $resData[] = [
                'time' => '',
                'color' => '#00E9A0',
                'title' => '点击查看星运变动',
                'target' => 'local',
                'link' => 'astro_foutune_change_list?tab_index=0&need_auth=' . ($isYoung ? 3 : 0),
            ];
        }

        return $resData;
    }

    /**
     * 获取星运变动   进行中
     * @param int $uid
     * @param array $recodeInfo
     * @param bool $isUnlock 本月 月运是否 已解锁
     * @param int $time
     * @return array
     * @throws \Exception
     */
    public function getUserAstroFortuneChangeUnderwayList(int $uid, array $recodeInfo, bool $isUnlock, int $time)
    {
        $astroFortuneChangeInfo = $recodeInfo ? (new Zl2AstrFortuneContent())->getUserAstroFortuneChangeData($uid, $recodeInfo, $time, 2) : [];
        if ($astroFortuneChangeInfo)
        {   // 开始解锁了
            // 档案是否更新
            $recodeIsChange = $astroFortuneChangeInfo['recode_is_change'] ? 1 : 0;
            if ($astroFortuneChangeInfo['status'] === 1)
            {   // 已解锁
                $data = $astroFortuneChangeInfo['content'];
                // 计算状态
                $status = 2;    // 已解锁
            } else
            {
                $data = $this->getAstroFortuneChangeUnderwayAstroData($time);
                $status = 1;    // 计算中

                if ($astroFortuneChangeInfo['status'] === 0)
                {   // 进行中
                    if ($time - $astroFortuneChangeInfo['create_time'] > 900)
                    {
                        // 重新发送 计算队列
                        Util::queuePush('astro_fortune_change_calculate', ['id' => $astroFortuneChangeInfo['id'], 'recode_info' => $recodeInfo], 0);
                    }
                } elseif ($astroFortuneChangeInfo['status'] === 2)
                {   // 计算失败
                    // 重新发送 计算队列
                    Util::queuePush('astro_fortune_change_calculate', ['id' => $astroFortuneChangeInfo['id'], 'recode_info' => $recodeInfo], 0);
                }
            }
        } else
        {   // 未计算过
            $data = $this->getAstroFortuneChangeUnderwayAstroData($time);
            $recodeIsChange = 0;
            $status = 0;
        }

        $codes = array_column($data, 'code');

        $codeTexts = (new Zl2AstrFortuneText())->batchGetInfoByIds($codes, ['code', 'explain', 'opportunity', 'challenge', 'extend', 'zan1', 'zan2', 'title']);
        $zanDatas = (new Zl2AstrFortuneTextZan())->getZan($uid);
        $resData = [];
        $luckAdversity = [
            1 => ['name' => '困难', 'color' => '#7775ff',],
            2 => ['name' => '警戒', 'color' => '#ffa536',],
            3 => ['name' => '平平', 'color' => '#ffed17',],
            4 => ['name' => '吉利', 'color' => '#00f4e7',],
            5 => ['name' => '大吉', 'color' => '#00e9a0',],
        ];
        $nowTime = time();
        $nowDate = date('Y-m-d');
        $nowTime0 = strtotime($nowDate);
        $num = 0;
        $envName = env('host.name');
        foreach ($data as $itemData)
        {
            if (!$isUnlock && $num >= 2)
            {   // 如果 月运未解锁   只显示两条数据
                break;
            }
            if ($nowTime > $itemData['et'] || $nowTime < $itemData['st'])
            { // 当前时间 大于 结束时间  或者运势还未开始
                continue;
            }
            $isZan = isset($zanDatas[$itemData['code']]) ? (int)$zanDatas[$itemData['code']] : 0;
            if (!isset($codeTexts[$itemData['code']]))
            {
                if ($envName === 'online')
                {   // 词条缺失 不显示
                    continue;
                }
                $hideData = [
                    [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => '词条缺失code=' . $itemData['code'],
                        ]
                    ]
                ];
                $keywords = [
                    [
                        'name' => '缺失',
                        'color' => $luckAdversity[1]['color'],
                    ]
                ];
                $tipIcon = $itemData['tag'] === '天象' ? '/test/astro_fortune/astro_status1.png' : '/test/astro_fortune/fortune_status1.png';
            } else
            {
                $tipIcon = $itemData['tag'] === '天象' ? "/test/astro_fortune/astro_status{$itemData['status']}.png" : "/test/astro_fortune/fortune_status{$itemData['status']}.png";
                $keywords = [];
                foreach ($codeTexts[$itemData['code']]['extend'] as $keywordsItem)
                {
                    if ($keywordsItem['title'])
                    {
                        $keywords[] = [
                            'name' => $keywordsItem['title'],
                            'color' => $luckAdversity[$keywordsItem['type']]['color'],
                        ];
                    }
                }

                $hideData = [
                    [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '持续时间',
                            'content' => sprintf("%s - %s", date('Y.m.d', $itemData['st']), date('Y.m.d', $itemData['et'])),
                        ]
                    ]
                ];
                if ($codeTexts[$itemData['code']]['explain'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => $codeTexts[$itemData['code']]['explain'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['opportunity'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '机遇',
                            'content' => $codeTexts[$itemData['code']]['opportunity'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['challenge'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '挑战',
                            'content' => $codeTexts[$itemData['code']]['challenge'],
                        ]
                    ];
                }
                if ($itemData['tag'] === '天象' && $itemData['score'] !== null)
                {
                    $des = '';
//                        if ($itemData['state'] === 3 && $itemData['st'] > $nowTime)
//                        {   // 结束的时候   并且 当前天象已经开始了
//                            $des = sprintf("还剩d%天", (int)(($itemData['et'] - $nowTime) / 86400));
//                        }
                    $hideData[] = [
                        'hide_type' => 3,
                        'data' => [
                            'title' => '你的感受度：',
                            'score' => (int)($itemData['score'] * 100),
                            'des' => $des,
                        ]
                    ];
                }
                $hideData[] = [
                    'hide_type' => 4,
                    'data' => [
                        'id' => $itemData['code'],
                        'zan_num1' => $codeTexts[$itemData['code']]['zan1'],
                        'zan_num2' => $codeTexts[$itemData['code']]['zan2'],
                        'is_zan' => $isZan,
                        'target' => 'local',
                        'link' => "astro_foutune_change_detail?id={$itemData['code']}&status={$itemData['status']}&date={$nowDate}&score={$itemData['score']}&need_auth=2",
                    ]
                ];
            }

            $endDay = (int)((strtotime(date('Y-m-d', $itemData['et'])) - $nowTime0) / 86400);

            $resData[] = [
                'type' => 1,
                'list_item' => [
                    'timestamp' => 0,
                    'time' => $itemData['time_des'],
                    'des' => sprintf("剩余%d天", $endDay),
                    'surprised_img' => '',
                    'label' => null,
                    'content' => [
                        [
                            'type' => 1,
                            'data' => [
                                'tip_icon' => $tipIcon,
                                'list' => $keywords,
                            ]
                        ], [
                            'type' => 4,
                            'data' => [
                                'style' => 1,
                                'tag' => $itemData['tag'],
                                'content' => $itemData['title'],
                                'img' => '',
                                'title' => '',
                                'read_num' => -1,
                                'synastry_num' => -1,
                                'hide_data' => $hideData,
                            ]
                        ],
                    ],
                ]
            ];
            $num++;
        }

        if (!$isUnlock)
        { // 未解锁
            $resData[] = [
                'type' => 3,
                'list_item' => [
                    'text' => '解锁月运可查看更多内容',
                    'target' => 'local',
                    'link' => 'fortune_for_month?rid=' . ($recodeInfo['id'] ?? 0) . '&time=' . date('Y-m') . '&need_auth=1',
                    'type' => 0
                ]
            ];
        } else
        {   // 已解锁
            if ($status === 0)
            {   // 未计算
                $resData[] = [
                    'type' => 3,
                    'list_item' => [
                        'text' => '点击计算当前进行中的个人运势',
                        'target' => 'api',
                        'link' => env('host.domain') . '/api/v35/astro_fortune_change/unlock?type=2',
                        'type' => 1
                    ]
                ];
            } elseif ($status === 1)
            {   // 计算中
                $resData[] = [
                    'type' => 4,
                    'list_item' => [
                        'text' => '计算中...',
                    ]
                ];
            }
        }

        return [
            'status' => $status,
            'recode_is_change' => $recodeIsChange,
            'data' => [
                'timestamp' => $time,
                'list' => $resData
            ],
        ];

    }


    /**
     * @param $uid
     * @param $rid
     * @param $date 2022-12-05
     * @param $type 1=>星象变动 2=>纯净 3=>进行中
     * @return array
     * @throws ApiException
     */
    public function getRecodeAstroFortuneChange($uid, $rid, $date, $type)
    {
        $nowTime0 = strtotime(date('Y-m-d'));    // 今天零点时间戳
        $timeRange = $this->getTimeRange();
        $recodeInfo = $rid > 0 ? $this->getrecodeInfo($rid) : [];
        switch ($type)
        {
            case 3:
                $dataType = 0;
//                $dataList[] = $this->getModeData2();
                $fortuneMonthUnlockData = $this->getUnlockMonthData($rid);

                $isUnlock = in_array(date('Y-n', time()), $fortuneMonthUnlockData, true);

                $astroFortuneChangeUnderwayData = $this->getUserAstroFortuneChangeUnderwayList($uid, $recodeInfo, $isUnlock, time());
                $status = $astroFortuneChangeUnderwayData['status'];
                $recodeIsChange = $astroFortuneChangeUnderwayData['recode_is_change'];
                $dataList[] = $astroFortuneChangeUnderwayData['data'];
                break;
            default:
                $dataType = 1;
                $dataList = [];
                $timeList = [];
                $dataTime = empty($date) ? $nowTime0 : strtotime($date);
                if ($dataTime < $timeRange['min'] || $dataTime >= $timeRange['max'])
                {
                    throw new ApiException('服务异常，超出可选时间范围');
                }

                if (in_array($dataTime, [$nowTime0 - 86400, $nowTime0, $nowTime0 + 86400], true))
                {   // 昨天 今天 明天
                    $surpriseCreate = $dataTime < $nowTime0 || $uid <= 0 || $rid <= 0 ? false : true; // 是否计算神秘惊喜
                    $timeList[] = $dataTime;
                } else
                {
                    if ($uid === 0)
                    {   // 未登录
                        throw new ApiException('前往登录', 200, 405);
                    }
                    if (!$rid)
                    {
                        throw new ApiException('档案缺失');
                    }
                    $fortuneMonthUnlockData = $this->getUnlockMonthData($rid);

                    if (in_array(date('Y-n', $dataTime), $fortuneMonthUnlockData, true))
                    {   // 已解锁
                        $timeList[] = $dataTime;
                    } else
                    {   // 未解锁  去解锁
                        $dataList[] = [
                            'timestamp' => $dataTime,
                            'list' => [
                                [
                                    'type' => 3,
                                    'list_item' => [
                                        'text' => '解锁月运可查看更多内容',
                                        'target' => 'local',
                                        'link' => 'fortune_for_month?need_auth=1&rid=' . $rid . '&time=' . date('Y-m', strtotime($date)),
                                    ]
                                ]
                            ]
                        ];

                        $recodeIsChange = 0;
                        $status = -1;
                    }
                    $surpriseCreate = false;
                }

                $userSurpriseRecordModel = new Zl2UserSurpriseRecord();
                $i = 0;
                $j = 0;
                $n = 5; // 每几条内容 一个神秘惊喜
                $surpriseTimes = [];
                foreach ($timeList as $k => $timeItem)
                {
                    $timeDateItem = (int)date('Ymd', $timeItem);
                    $list = [];
                    // 获取星运变动信息
                    $astroFortuneChangeData = $this->getUserAstroFortuneChangeDataResert($uid, $recodeInfo, $timeItem, $nowTime0, $type);
                    $status = $astroFortuneChangeData['status'];
                    $recodeIsChange = $astroFortuneChangeData['recode_is_change'];
                    $astroFortuneChangeData = $astroFortuneChangeData['data'];

                    if ($type === 1)
                    {   // 获取 神秘惊喜
                        $surpriseRecordData = $uid ? $userSurpriseRecordModel->getUserSurpriseRecordData($uid, $timeDateItem) : [];
                        if ($surpriseCreate && $status === 2)
                        {   // 需要创建惊喜
                            $user = (new \App\Models\Api\User())->getUserinfo(['id' => $uid]);
                            $astroFortuneSurpriseServer = AstroFortuneSurprise::getInstance()->setUserInfo($user);
                            // 准备开始生成神秘惊喜
                            if (!$surpriseRecordData)
                            {
                                foreach ($astroFortuneChangeData as $k1 => $datum)
                                {
                                    if ($datum['list_item']['label']['text'] = '开始')
                                    {
                                        $i++;
                                        if ($i >= $n)
                                        {
                                            $i = 0;
                                            $j++;
                                            if (isset($astroFortuneChangeData[$k1 + 1]))
                                            {   // 如果后面还有内容
                                                $t1 = $astroFortuneChangeData[$k1 + 1]['list_item']['timestamp'] - $datum['list_item']['timestamp'];
                                                if ($t1 > 0)
                                                {
                                                    $st = mt_rand(1, $t1);
                                                } else
                                                {
                                                    $st = 1;
                                                }
                                            } else
                                            {   // 当天最后一条数据
                                                $t1 = $timeItem - $datum['list_item']['timestamp'];
                                                $st = $t1 > 1 ? mt_rand(1, $t1) : 1;
                                            }
                                            $surpriseTimes[] = $datum['list_item']['timestamp'] + $st;
                                        }
                                    }
                                }
                                if ($surpriseTimes)
                                {
                                    $surpriseCreateDatas = $astroFortuneSurpriseServer->getSurprise(0, count($surpriseTimes));

                                    $surpriseRecordData = [];
                                    foreach ($surpriseTimes as $key => $surpriseTime)
                                    {
                                        $surpriseCreateDataItem = $surpriseCreateDatas[$key];
                                        $insertData = [
                                            'uid' => $uid,
                                            'status' => 0,
                                            'type' => $surpriseCreateDataItem['type'],
                                            'sid' => $surpriseCreateDataItem['id'],
                                            'sid_node_id' => $surpriseCreateDataItem['sid_node_id'],
                                            'user_cid' => 0,
                                            'date' => (int)date('Ymd', $surpriseTime),
                                            'start_time' => $surpriseTime,
                                            'create_time' => time(),
                                        ];
                                        // 生成惊喜数据
                                        $sid = $userSurpriseRecordModel->insertGetId($insertData);
                                        $insertData['id'] = (int)$sid;

                                        $surpriseRecordData[] = $insertData;
                                    }
                                    // 删除当天惊喜数据缓存
                                    CacheManage::getInstance()->setCacheConfig('user.surprise_record', $uid)->setListKeyType($timeDateItem)->rmCache('list');
                                }
                                $j = 0;
                                $surpriseTimes = [];
                                $surpriseRecordData = $userSurpriseRecordModel->astroFortuneChangeDataResert($surpriseRecordData);
                            }
                        }

                        if ($timeItem === $nowTime0)
                        {   // 当天时间  ------------------------------
                            $list[] = [ // 添加当前时间时间线
                                'type' => 2,
                                'list_item' => [
                                    'timestamp' => time(),
                                    'time' => date('H:i'),
                                    'time_des' => '当前时间'
                                ]
                            ];
                            // 添加数据月运     -判断本月月运是否解锁
                            $fortuneData = $rid ? $this->getFortuneMonthIndoData($rid, $timeItem) : [];
                            if ($fortuneData)
                            {
                                $list[] = $fortuneData;
                            }
                            // 添加 运势笔记数据
                            $list[] = $this->getFortuneNoteData($uid, $rid, $timeItem);
                        }
                        // 获取准时提醒数据
                        $timeNote = $uid ? $this->getTimeNoteData($uid, $timeItem) : [];
                        // 获取生日信息
                        $userBrithData = $uid ? $this->getUserBrithdayData($uid, $timeItem) : [];
                    } else
                    {
                        $surpriseRecordData = [];
                        $timeNote = [];
                        $userBrithData = [];
                    }
                    // 合并数据 ------------------------
                    $list = array_merge($list, $astroFortuneChangeData, $surpriseRecordData, $timeNote, $userBrithData);

                    $list = resort3($list, 'list_item.timestamp', 'asc');

                    $dataList[] = [
                        'timestamp' => $timeItem,
                        'list' => $list
                    ];
                }
        }

        $unlockTime = $rid && $type !== 3 ? (new Zl2AstrFortuneContent())->getUnlockDateData($uid, $rid, 1) : [];

        $resData = [
            'type' => $dataType,
            'status' => $status,
            'recode_is_change' => $recodeIsChange,
            'unlock_time' => $unlockTime,
            'time_range' => $timeRange,
            'date_list' => $dataList,
            'server_time' => time(),
        ];
        return $resData;
    }

    /**
     * 获取档案信息
     * @param int $rid
     * @return mixed
     * @throws \Exception
     */
    private function getrecodeInfo(int $rid)
    {
        if (isset($this->recodeInfo[$rid]))
        {
            return $this->recodeInfo[$rid];
        }
        $recodeInfo = (new Recode())->getInfoById($rid);

        $this->recodeInfo[$rid] = $recodeInfo;

        return $recodeInfo;
    }

    /**
     * 获取 记录生活 数据    跳转到添加运势笔记
     * @param int $timeItem
     * @return array
     * @throws \Exception
     */
    private function getFortuneNoteData($uid, $rid, int $timeItem)
    {
        if ($uid && $rid)
        {
            $arr = FortuneCache::CheckFortuneSevenUnlockStatus($uid, $rid, $timeItem);
        } else
        {
            $arr['note_status'] = 0;
        }
        if (Util::CheckAppVersion('2.9.7') === '大')
        {
            $link = 'mood_index?need_auth=2';
        } else
        {
            $link = $arr['note_status'] ? 'fortune_day_note?need_auth=1' : 'fortune_day_note_add?time' . date('Y-m-d') . '=&need_auth=1';
        }
        return [
            'type' => 1,
            'list_item' => [
                'timestamp' => strtotime(date('Y-m-d 21:00:00')),
                'time' => '21:00',
                'des' => '',
                'surprised_img' => '',
                'label' => [
                    'text' => '记',
                    'bg_color' => '#00D8D3',
                ],
                'content' => [
                    [
                        'type' => 2,
                        'data' => [
                            'icon' => '/test/astro_fortune/remark_icon.png',
                            'title' => '记录生活',
                            'tip_icon' => '',
                        ]
                    ], [
                        'type' => 3,
                        'data' => [
                            'content' => '记录一下今天的生活吧～',
                            'target' => 'local',
                            'link' => $link,
                            'is_arrow' => 1,
                            'sign_name' => '',
                            'relation_name' => '',
                        ]
                    ],
                ],
            ],
        ];
    }

    /**
     * 获取 档案 解锁的月运信息
     * @param int $rid
     * @return array|false[]|mixed|string[]|null
     * @throws \Exception
     */
    private function getUnlockMonthData($rid)
    {
        if (is_null($this->unlockMonthData))
        {
            $unlockMonths = $rid ? (new MonthFortune())->getReocdeFortuneUnlockMonths($rid) : [];

            $time_range = array_map(function ($timeValue)
            {
                return date('Y-n', strtotime($timeValue));
            }, $unlockMonths);

            $this->unlockMonthData = $time_range;

            return $time_range;
        }

        return $this->unlockMonthData;
    }

    /**
     * 获取月运数据
     * @param int $rid
     * @param int $timeItem
     * @return array
     * @throws \Exception
     */
    private function getFortuneMonthIndoData(int $rid, int $timeItem)
    {
        $time_range = $this->getUnlockMonthData($rid);

        if (in_array(date('Y-n', $timeItem), $time_range, true))
        {   // 下月已解锁
            $indexMonth = date('Y-m', $timeItem);
            $recodeInfo = $this->getrecodeInfo($rid);
            $fortuneDetail = (new FortunesChange())->init2($recodeInfo, $indexMonth);
            if ((int)$fortuneDetail['status'] === 2 && $fortuneDetail['fortune']['underway_fortune'])
            {   // 已解锁
                $indexFortune = $fortuneDetail['fortune']['underway_fortune'][0];
                $start_time = implode('.', array_slice(explode('.', $indexFortune['start_time']), 1));
                $end_time = implode('.', array_slice(explode('.', $indexFortune['end_time']), 1));
                return [
                    'type' => 1,
                    'list_item' => [
                        'timestamp' => strtotime(date('Y-m-d 12:00:00', $timeItem)),
                        'time' => '12:00',
                        'des' => '',
                        'surprised_img' => '',
                        'label' => [
                            'text' => '月',
                            'bg_color' => '#00D8D3',
                        ],
                        'content' => [
                            [
                                'type' => 2,
                                'data' => [
                                    'icon' => $indexFortune['icon'],
                                    'title' => $indexFortune['title'],
                                    'tip_icon' => '/test/astro_fortune/month_fortune_tip_icon.png',
                                ]
                            ], [
                                'type' => 3,
                                'data' => [
                                    'content' => "{$start_time} - {$end_time}",
                                    'target' => 'local',
                                    'link' => 'fortune_for_month?need_auth=1&rid=' . $rid . '&time=' . $indexMonth,
                                    'is_arrow' => 1,
                                    'sign_name' => '',
                                    'relation_name' => '',
                                ]
                            ],
                        ],
                    ]
                ];
            }
        }
        // 未解锁
        return [];
    }

    /**
     * 获取用户 档案生日信息
     * @param int $timeItem
     * @return array
     * @throws \Exception
     */
    private function getUserBrithdayData($uid, int $timeItem)
    {
        $user = (new \App\Models\Api\User())->getUserinfo(['id' => $uid]);
        $data = (new Recode())->userBirthDayList($user, date('Y-m-d', $timeItem));

        $resData = [];
        $host = env('host.domain');
        foreach ($data as $itemData)
        {
            $resData[] = [
                'type' => 1,
                'list_item' => [
                    'timestamp' => strtotime(date('Y-m-d 10:00:00', $timeItem)),
                    'time' => '10:00',
                    'des' => '',
                    'surprised_img' => '',
                    'label' => [
                        'text' => '生',
                        'bg_color' => '#00D8D3',
                    ],
                    'content' => [
                        [
                            'type' => 2,
                            'data' => [
                                'icon' => '/test/astro_fortune/birthday_icon.png',
                                'title' => $itemData['type'] === 1 ? '祝你生日快乐' : '生日提醒',
                                'tip_icon' => '',
                            ]
                        ], [
                            'type' => 3,
                            'data' => [
                                'content' => $itemData['type'] === 1 ? '快来领取你的生日礼包～' : "今天是{$itemData['name']}的生日～",
                                'target' => 'link',
                                'link' => $host . $itemData['link'],
                                'is_arrow' => 1,
                                'sign_name' => '',
                                'relation_name' => '',
                            ]
                        ],
                    ],
                ],
            ];
        }
        return $resData;
    }

    /**
     * 获取准时提醒的数据
     * @param int $timeItem
     * @return array
     * @throws \Exception
     */
    private function getTimeNoteData($uid, int $timeItem)
    {
        if (is_null($this->timeNoteData))
        {
            request()->get(['page' => -1]);
            $timeNoteModel = new Zl2TimeNote();
            $pageData = $timeNoteModel->getListPage($uid, 'id', ['id', 'time', 'note']);
            $this->timeNoteData = $pageData['data'];
            $timeNoteData = $pageData['data'];
        } else
        {
            $timeNoteData = $this->timeNoteData;
        }
        $listData = [];
        $maxTime = $timeItem + 86400;
        foreach ($timeNoteData as $datum)
        {
            if ($datum['time'] > $timeItem && $datum['time'] < $maxTime)
            {   // 当天的 准时提醒 数据
                $listData[] = [
                    'type' => 1,
                    'list_item' => [
                        'timestamp' => (int)$datum['time'],
                        'time' => date('H:i', $datum['time']),
                        'des' => '',
                        'surprised_img' => '',
                        'label' => [
                            'text' => '记',
                            'bg_color' => '#00D8D3',
                        ],
                        'content' => [
                            [
                                'type' => 2,
                                'data' => [
                                    'icon' => '/test/astro_fortune/remind_icon.png',
                                    'title' => '“准”时提醒',
                                    'tip_icon' => '',
                                ]
                            ], [
                                'type' => 3,
                                'data' => [
                                    'content' => $datum['note'],
                                    'target' => '',
                                    'link' => '',
                                    'is_arrow' => 0,
                                    'sign_name' => '',
                                    'relation_name' => '',
                                ]
                            ],
                        ],
                    ]
                ];
            }
        }
        return $listData;
    }

    /**
     * 获取 进行中 的 天象信息 【当前仅心情笔记首页显示】
     * @param int $uid
     * @param int $time
     * @param int $status 1=>好（大笑） 2=>平（微笑） 3=>坏（哭）
     * @param int $maxNum
     * @return array
     * @throws \Exception
     */
    public function getAstroFortuneChangeUnderwayList(int $uid, int $time, int $status, int $maxNum = 3)
    {
        $data = $this->getAstroFortuneChangeData($time);
        $ItemDate = date('Y-m-d', $time);

        $codes = array_column($data, 'code');
        $codeTexts = (new Zl2AstrFortuneText())->batchGetInfoByIds($codes, ['code', 'explain', 'opportunity', 'challenge', 'extend', 'zan1', 'zan2', 'title']);
        $zanDatas = (new Zl2AstrFortuneTextZan())->getZan($uid);
        $resData = [];
        $luckAdversity = [
            1 => ['name' => '困难', 'color' => '#7775ff',],
            2 => ['name' => '警戒', 'color' => '#ffa536',],
            3 => ['name' => '平平', 'color' => '#ffed17',],
            4 => ['name' => '吉利', 'color' => '#00f4e7',],
            5 => ['name' => '大吉', 'color' => '#00e9a0',],
        ];
        $num = 0;
        $envName = env('host.name');
        foreach ($data as $itemData)
        {
            // $itemData['status'] // 1=>好（大笑） 2=>平（微笑） 3=>坏（哭）
            if ($status !== $itemData['status'] || $itemData['data_type'] === 2) continue;
            if ($num >= $maxNum)
            {   // 如果 月运未解锁   只显示两条数据
                break;
            }
            // 数据类型 1=>天象信息
            $isZan = isset($zanDatas[$itemData['code']]) ? (int)$zanDatas[$itemData['code']] : 0;
            if (!isset($codeTexts[$itemData['code']]))
            {
                if ($envName === 'online')
                {   // 词条缺失 不显示
                    continue;
                }
                $hideData = [
                    [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => '词条缺失code=' . $itemData['code'],
                        ]
                    ]
                ];
                $keywords = [
                    [
                        'name' => '缺失',
                        'color' => $luckAdversity[1]['color'],
                    ]
                ];
                $tipIcon = $itemData['tag'] === '天象' ? '/test/astro_fortune/astro_status1.png' : '/test/astro_fortune/fortune_status1.png';
            } else
            {
                $tipIcon = $itemData['tag'] === '天象' ? "/test/astro_fortune/astro_status{$itemData['status']}.png" : "/test/astro_fortune/fortune_status{$itemData['status']}.png";
                $keywords = [];
                foreach ($codeTexts[$itemData['code']]['extend'] as $keywordsItem)
                {
                    if ($keywordsItem['title'])
                    {
                        $keywords[] = [
                            'name' => $keywordsItem['title'],
                            'color' => $luckAdversity[$keywordsItem['type']]['color'],
                        ];
                    }
                }

                $hideData = [];
                if ($codeTexts[$itemData['code']]['explain'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '解读',
                            'content' => $codeTexts[$itemData['code']]['explain'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['opportunity'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '机遇',
                            'content' => $codeTexts[$itemData['code']]['opportunity'],
                        ]
                    ];
                }
                if ($codeTexts[$itemData['code']]['challenge'])
                {
                    $hideData[] = [
                        'hide_type' => 1,
                        'data' => [
                            'title' => '挑战',
                            'content' => $codeTexts[$itemData['code']]['challenge'],
                        ]
                    ];
                }
                if ($itemData['score'] !== null)
                {
                    $des = '';
//                        if ($itemData['state'] === 3 && $itemData['st'] > $nowTime)
//                        {   // 结束的时候   并且 当前天象已经开始了
//                            $des = sprintf("还剩d%天", (int)(($itemData['et'] - $nowTime) / 86400));
//                        }
                    $hideData[] = [
                        'hide_type' => 3,
                        'data' => [
                            'title' => '你的感受度：',
                            'score' => (int)($itemData['score'] * 100),
                            'des' => $des,
                        ]
                    ];
                }
//                    if ($itemData['time'] < $maxTime)
//                    {   // 后天以后的时间   不显示
                $hideData[] = [
                    'hide_type' => 4,
                    'data' => [
                        'id' => $itemData['code'],
                        'zan_num1' => $codeTexts[$itemData['code']]['zan1'],
                        'zan_num2' => $codeTexts[$itemData['code']]['zan2'],
                        'is_zan' => $isZan,
                        'target' => 'local',
                        'link' => "astro_foutune_change_detail?id={$itemData['code']}&status={$itemData['status']}&score={$itemData['score']}&date={$ItemDate}&need_auth=2",
                    ]
                ];
//                    }
            }

            $resData[] = [
                'type' => 1,
                'list_item' => [
                    'timestamp' => $itemData['time'],
                    'time' => date('H:i', $itemData['time']),
                    'des' => $itemData['state'] === 1 ? '开始' : '结束',
                    'surprised_img' => '',
                    'label' => $itemData['state'] === 1 ? ['text' => '始', 'bg_color' => '#4BEA9C',] : ['text' => '终', 'bg_color' => '#FFB500',],
                    'content' => [
                        [
                            'type' => 1,
                            'data' => [
                                'tip_icon' => $tipIcon,
                                'list' => $keywords,
                            ]
                        ], [
                            'type' => 4,
                            'data' => [
                                'style' => 1,
                                'tag' => $itemData['tag'],
                                'content' => $itemData['title'],
                                'img' => '',
                                'title' => '',
                                'read_num' => -1,
                                'synastry_num' => -1,
                                'hide_data' => $hideData,
                            ]
                        ],
                    ],
                ]
            ];
            $num++;
        }

        return $resData;
    }
}
